/**
 * 用户中心--发票--索取发票
 */
import Pagination from "../../../../components/Pagination"
import {
    addInvoiceInvoice,
    addrInfoList,
    getInvoiceInfoList,
    searchInvoiceOrderList,
    searchInvoiceOrderListByParam
} from "../../../../api"

export default {
    name: "invoice-request",
    components: {Pagination},
    data() {

        return {
            searchOrderId: null,//查询条件时的订单号
            searchMaxPrice: null,//查询条件时的最高价
            searchminPrice: null,//查询条件时的最低价
            radio: 3,
            invoiceType: 1,
            transmitAddrData: {},
            defaultAddr: {},
            invoiceInfo: {
                invoiceType: '增值税普通发票',
                makeInvoice: 1,
                invoiceTitle: '',
                orderids: '',
                taxNum: '',
                invoicePrice: 0,
                status: false,
                bankName: '',
                phone: '',
                bankNum: '',
            },
            unfoldFlag: false,
            multipleSelection: [],//已选的订单
            chosenAll: false,
            chosenOrdersVisible: false,
            showMoreAddr: false,//展开更多收货地址
            active: 2,
            orderData: [],
            page: 1,
            total: 0,
            size: 10
        }
    },
    created() {
        this.searchOrderList()
    },
    methods: {
        //返回
        goBack() {
            //返回发票首页
            this.$emit('moduleIndexChange', 'home')
        },
        //复选框change
        handleSelectionChange(val) {
            console.log(val)
            this.multipleSelection = val
            //整理选中的订单数据
            let p = 0
            let ids = ''
            this.multipleSelection.forEach(item => {
                p += item.realsumPrice
                ids += ids == '' ? item.orderId : ',' + item.orderId
            })
            this.invoiceInfo.invoicePrice = p
            this.invoiceInfo.orderids = ids
        },
        //下一步
        nextStep() {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    message: '订单不能为空',
                    type: 'error'
                })
                return
            }
            this.active = 3
            this.searchAddrList()
            this.searchInvoiceInfo()
            this.searchAddr()
        },
        //查询默认发票信息
        searchInvoiceInfo() {
            let param = {
                status: 1
            }
            getInvoiceInfoList(param).then(res => {
                if (res.data.code === 200) {
                    let r = res.data.result[0]
                    this.invoiceInfo.invoiceType = r.invoiceType
                    this.invoiceInfo.makeInvoice = r.makeInvoice
                    this.invoiceInfo.invoiceTitle = r.invoiceTitle
                    this.invoiceInfo.taxNum = r.taxNum
                    this.invoiceInfo.bankName = r.bankName
                    this.invoiceInfo.phone = r.phone
                    this.invoiceInfo.bankNum = r.bankNum
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取列表失败！")
            })
        },
        //查询默认收货地址
        searchAddr() {
            let param = {
                status: 1
            }
            addrInfoList(param).then(res => {
                if (res.data.code === 200) {
                    if (res.data.result.length > 0) {
                        this.defaultAddr = res.data.result[0]
                    }
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("获取收货地址失败！")
            })
        },
        //查询收货地址
        searchAddrList() {
            let param = {
                status: null
            }
            addrInfoList(param).then(res => {
                if (res.data.code === 200) {
                    this.transmitAddrData = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("查询地址失败！")
            })
        },
        //查询课索取发票的订单
        searchOrderList() {
            searchInvoiceOrderList({
                status:2
            }).then(res => {
                if (res.data.code === 200) {
                    this.orderData = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("查询开票订单失败！")
            })
        }, //查询课索取发票的订单(带条件)
        searchOrderListByparam() {
            let param = {
                orderId: this.searchOrderId,
                maxPrice: this.searchMaxPrice,
                minPrice: this.searchminPrice,
                status:2
            }
            searchInvoiceOrderListByParam(param).then(res => {
                if (res.data.code === 200) {
                    this.orderData = res.data.result
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("查询开票订单失败！")
            })
        },
        //整理商品名称
        getOrderCommodityNames(list) {
            let name = ''
            list.forEach(n => {
                name += name == '' ? n.projectName : '，' + n.projectName
            })
            return name
        },
        //价格输入框change
        priceChange() {
            if (this.searchMaxPrice == null || this.searchminPrice == null) {
                return
            }
            if (this.searchMaxPrice < this.searchminPrice) {
                this.$message({
                    message: "最低价不能高于最高价",
                    type: 'warning'
                })
                return
            }
            this.searchOrderListByparam()
        },

        //索要发票
        comfirmInvoice() {

            if (!this.defaultAddr.recipients) {
                this.$message({
                    message: "请先新增默认地址",
                    type: 'warning'
                })
                return
            }

            this.invoiceInfo.status = 1
            addInvoiceInvoice(this.invoiceInfo).then(res => {
                if (res.data.code === 200) {
                    this.goBack()
                    this.$message({
                        message: '操作成功',
                        type: 'success'
                    })
                } else {
                    this.$message({
                        message: res.data.msg,
                        type: 'warning'
                    })
                }
            }).catch(() => {
                // this.$message.error("操作失败！")
            })
        },


    }

}
